package com.ckm.controller;

import com.ckm.annotation.LogInfo;
import com.ckm.entity.Major;
import com.ckm.entity.ResponseResult;
import com.ckm.service.MajorService;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@LogInfo
@Validated
@RestController
@RequestMapping("major")
public class MajorController {

    @Autowired
    private MajorService majorService;

    /**
     * 添加学院
     * @param majorList 学院信息系list
     * @return 成功信息
     */
    @PostMapping("addMajor")
    public ResponseResult addMajor(@RequestBody @Valid List<Major> majorList) {
        majorService.addMajor(majorList);
        return new ResponseResult(200,"添加成功",null);
    }

    /**
     * 更新学院
     * @param major 更新的信息
     * @return 成功信息
     */
    @PutMapping
    public ResponseResult updateMajor(@RequestBody Major major) {
        majorService.updateMajor(major);
        return new ResponseResult(200,"更新成功","");
    }

    /**
     * 删除学院
     * @param id 学院Id
     * @return 成功信息
     */
    @DeleteMapping
    public ResponseResult deleteMajor(@RequestParam(name = "id") Integer id) {
        majorService.deleteMajor(id);
        return new ResponseResult(200,"删除成功","");
    }

    /**
     * 查找专业信息
     * @param major 查找词
     * @param pageNum 页码
     * @param pageSize 页大小
     * @return 分页结果集
     */
    @PostMapping("find")
    public ResponseResult findMajor(@RequestBody Major major, @RequestParam(name = "pageNum",defaultValue = "1") int pageNum, @RequestParam(name = "pageSize", defaultValue = "10") int pageSize) {
        return new ResponseResult(200,"查找完毕",majorService.getMajorPage(major, pageNum, pageSize));
    }

    /**
     * 统计学院中有的专业
     * @param colName 学院名称
     * @return 专业集合
     */
    @GetMapping("statistics")
    public ResponseResult statistics(String colName){
        return new ResponseResult(200,"统计学院的专业",majorService.statistics(colName));
    }
}
